import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;

class Student implements Comparable<Student> {
    public int age;

    public Student(int age) {
        this.age = age;
    }

    @Override
    public int compareTo(Student o) {
        return this.age - o.age; //小根堆存储
//        return o.age - this.age; //大根堆
    }
}

class IntCmp implements Comparator<Integer> { //自定义比较器

    @Override
    public int compare(Integer o1, Integer o2) {
        return o2.compareTo(o1); //o2-o1
    }
}

public class Test {
    public static void main1(String[] args) {
        TestHeap testHeap = new TestHeap();
        int[] array = {27, 15, 19, 18, 28, 34, 65, 49, 25, 37};
        testHeap.initElem(array);

        testHeap.createHeap(); //创建大根堆
        System.out.println(Arrays.toString(testHeap.elem)); //打印创建好的大根堆

        testHeap.offer(50); //插入节点，并且会形成大根堆
        System.out.println(Arrays.toString(testHeap.elem));

        System.out.println(testHeap.pop()); //删除节点

        System.out.println(testHeap.peek()); //返回根节点
    }

    public static void main2(String[] args) {
        //堆 -- 默认小根堆
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
        priorityQueue.offer(10);
        priorityQueue.offer(20);
        priorityQueue.offer(3);

        System.out.println(priorityQueue.poll());
        System.out.println(priorityQueue.poll());
        System.out.println(priorityQueue.poll());
    }

    public static void main3(String[] args) {
        PriorityQueue<Student> priorityQueue = new PriorityQueue<>();
        priorityQueue.offer(new Student(10));
        priorityQueue.offer(new Student(5));
    }

    public static void main4(String[] args) {
        //优先级队列 -- 堆存储
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(new IntCmp()); //改为了大根堆
        priorityQueue.offer(10);
        priorityQueue.offer(20);
        priorityQueue.offer(3);

        System.out.println(priorityQueue.poll());
        System.out.println(priorityQueue.poll());
        System.out.println(priorityQueue.poll());
    }

    /* 总结：
        1、当没有传入数组容量时，默认是11
        2、当没有传入比较器时，传入的参数必须是可比较的
        3、优先使用比较器来比较
     */



    public static void main5(String[] args) {
        ArrayList<Integer> arrayList = new ArrayList<>();
        arrayList.add(11);
        arrayList.add(5);
        arrayList.add(9);
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(arrayList);
        System.out.println(priorityQueue); //小根堆
    }
}
